package com.lagou;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

@Component
public class MyWebApplicationInitializer implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    //@Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // Create and register the DispatcherServlet
        //基于java代码的方式初始化DispatcherServlet
        //DispatcherServlet servlet = new DispatcherServlet(ac);
        DispatcherServlet servlet = (DispatcherServlet) context.getBean(DispatcherServlet.class);
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");

//        HomeServlet homeServlet = new HomeServlet();
//        registration = servletContext.addServlet("home", homeServlet);
//        registration.setLoadOnStartup(1);
//        registration.addMapping("/home/*");
    }


}
